define(['template/mobile/list','qrcode','wx','wx.ajax','wx.tpl','wx.pop','wx.validator'],function(tpl,qrcode,wx){
	var mobile = {};
	var windowId;
	var listData;
	var getUrl = frontWebUrl+"/activity/:id.html";
    function init(id){
    	windowId = wx.stringToJson(id).windowId;
        var t = new Date().getTime(),params ={};
		params.currentPage = 1;
		params.pageSize = 10;
        $.when(
            $.ajax('/web/res/css/mobile_edit.css?_='+t),
            $.ajax('/web/res/anicss/animate.css?_='+t))
            .done(function(css,aniCss){
                $('<style type="text/css">').append(css,aniCss).appendTo('head');
				wx.sendData('/mobileh5/query.action',{type:'get',},function(data){
						if(data.status == "0" || data.status == "2"){
							listData = data.result;
							if(data.result.length > 0){
								data.url=getUrl.replace(':id',data.result[0]._id);
							}
							$('#window-frame-mobile').html(wx.tpl(tpl.list,data));
							$('#Js-mobile-list').height($('#window-frame-mobile .editList').height()-65);

							if(listData.length){
								$('#Js-iframe').attr('src',data.url);
								setStatusBar(data.result[0]);
							}
							bindEvent();
						}else{
							alert(data.message);
						}

					});
            });
	}

	function setStatusBar(data){
		var modifyInfo = '';
		if(data.create_by)
			modifyInfo='上次修改人：'+data.modify_by+'&nbsp;&nbsp;&nbsp;&nbsp;     修改时间：'+data.modify_time+'&nbsp;&nbsp;&nbsp;&nbsp; | &nbsp;&nbsp;&nbsp;&nbsp;';
		$('#window-bar-mobile').html(modifyInfo+'创建人：'+data.create_by+'&nbsp;&nbsp;&nbsp;&nbsp;     创建时间：'+data.create_time);
	}

	var addPop;
	function bindEvent(){
		$('.Js-li').click(function(){
			var id = $(this).attr('data-id')||0;
			$(this).siblings().removeClass("active");
			$(this).addClass("active");
			setStatusBar(listData[$(this).index()]);
			$('#Js-iframe').attr('src',getUrl.replace(':id',id));
		});

		$('#Js-mobile-add').click(function(){
			addPop = wx.pop(tpl.add);
			wx.validator();
			$('form[name="mobileAddForm"]').on('validator',addnew);
		});
		$('.Js-mobile-edit').click(function(){
    		Core.close(windowId);
            Core.create('编辑',{title:'H5工具-编辑',url:'mobileedit',width:'100%',height:'100%',resize:false,imgsrc:'/web/res/images/icos/ico.png',param:'parentId='+windowId+'&id='+$(this).parent().data('id')});
		});
		$('.Js-erwei').hover(function(){
			$(this).parent().siblings('.Js-code').fadeIn('fast');
		},function(){
			$(this).parent().siblings('.Js-code').fadeOut('fast');
		});
		$('.Js-li').hover( function(event){
			$(this).find('.Js-hover').show();
			
			if($(this).find('.Js-code canvas').length == 0){
				var qrcode = new QRCode($(this).find('.Js-code')[0], {
		            width : 175,
		            height : 175
		        });
		        qrcode.makeCode(getUrl.replace(':id',$(this).attr('data-id')));
			}
		}, function(event){
		   	$(this).find('.Js-hover').hide();
		});

		$('.Js-mobile-del').click(function(){
			var item = $(this).parents('li');
			var id = item.data('id');
			wx.confirm('是否要删除此项目？',{fn:function(){
				wx.sendData('/mobileh5/del.action?_id='+id,{},function(data){
					if(data.status == 0){
						item.remove();
					} else {
						wx.alert(data.info);
					}
				})
			}})
		});
	}

	function addnew(event,data){
		addPop.close();
    	Core.close(windowId);
	    Core.create('新增',{title:'H5工具-编辑',url:'mobileedit',width:'100%',height:'100%',resize:false,imgsrc:'/web/res/images/icos/ico.png',param:'parentId='+windowId+'&id='+data.result._id});
	}
	mobile.init = init;
    return mobile;
});